/*     Copyright (c) 2018, jymenu.com. All rights reserved.   */
package com.yunhaikeji.core.util;

import lombok.Builder;
import lombok.Data;

import java.time.*;
import java.util.Calendar;
import java.util.Date;

/**
 * CalendarUtils
 *
 * @author : zhuo
 * @date : 2018/8/20 20:55
 * @description :
 */
public class CalendarUtils {
    /**
     * 用来全局控制 上一周，本周，下一周的周数变化
     */
    private int weeks = 0;
    /**
     * 一月最大天数
     */
    private int MaxDate;
    /**
     * 一年最大天数
     */
    private int MaxYear;

    @Data
    @Builder
    public static class DayCompare{
        private int year;
        private int month;
        private int day;
    }

    /**
     * 月末日
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 设为当前月的1号
        calendar.set(Calendar.DATE, 1);
        // 加一个月，变为下月的1号
        calendar.add(Calendar.MONTH, 1);
        // 减去一天，变为当月最后一天
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    public static boolean isSameMonth(Date sourceDate, Date targetDate) {
        Calendar source = Calendar.getInstance();
        source.setTime(sourceDate);
        Calendar target = Calendar.getInstance();
        target.setTime(targetDate);
        return source.get(Calendar.MONTH) == target.get(Calendar.MONTH)
                && source.get(Calendar.YEAR) == target.get(Calendar.YEAR);
    }

    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 设为当前月的1号
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }

    public static int getDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 环月
     * @param date
     * @return
     */
    public static Date getSameDayOfPreMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 减一个月，变为上月
        calendar.add(Calendar.MONTH, -1);
        return calendar.getTime();
    }

    public static Date getSameDayOfPreYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 减一个年，变为上年
        calendar.add(Calendar.YEAR, -1);
        return calendar.getTime();
    }



    public static DayCompare dayComparePrecise(Date fromDate,Date toDate){
        LocalDate from = UDateToLocalDate(fromDate);
        LocalDate to = UDateToLocalDate(toDate);

        Period period = Period.between(from, to);

        int years = period.getYears();
        int months = period.getMonths();
        int days = period.getDays();
        return DayCompare.builder().day(days).month(months).year(years).build();
    }

    public static String dayComparePreciseString(Date fromDate, Date toDate) {
        String days = "";
        DayCompare dayCompare = dayComparePrecise(fromDate, toDate);
        if (dayCompare.getYear() != 0) {
            days += dayCompare.getYear() + "年";
        }
        if (dayCompare.getMonth() != 0) {
            days += dayCompare.getMonth() + "个月";
        }
        if (dayCompare.getDay() != 0) {
            days += dayCompare.getDay() + "天";
        }
        return days;
    }

    public static LocalDate UDateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    public static  int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;
    }

    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("当前日期：" + DateUtils.formatDate(date));
        System.out.println("当前日期的月末日期：" + DateUtils.formatDate(getLastDayOfMonth(date)));
        System.out.println("当前日期的上月日期：" + DateUtils.formatDate(getSameDayOfPreMonth(date)));
        System.out.println("当前日期的上年日期：" + DateUtils.formatDate(getSameDayOfPreYear(date)));
        System.out.println(dayComparePreciseString(DateUtils.fmtStrToDate("2012-12-31"), new Date()));
        System.out.println(isSameMonth(DateUtils.fmtStrToDate("2018-12-12"), new Date()));
        System.out.println(getAge(DateUtils.fmtStrToDate("2020-12-12")));
    }


}
